package com.pheonix.events;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Minutes;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

public class DayLineLayout extends RelativeLayout {

	final LocalDate date;

	final String day;
	final String monthDay;
	final String year;
	
	static Paint hLine = new Paint();
	static Paint vLine = new Paint();
	static Paint border = new Paint();
	static Path mPath = new Path();
	
	static LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 4320);

	static {
		hLine.setStrokeWidth(3);
		hLine.setColor(Color.GRAY);
		hLine.setStyle(Paint.Style.STROKE);
		hLine.setPathEffect(new DashPathEffect(new float[] {10,10}, 5));
		
		vLine.setColor(Color.GRAY);
		vLine.setTextSize(25);
		vLine.setTextAlign(Align.CENTER);
		
		border.setStrokeWidth(10);
	}
	
	public DayLineLayout(Context context, LocalDate date) {
		super(context);

		this.date = date;
		
		this.day = date.dayOfWeek().getAsShortText();
		this.monthDay = date.monthOfYear().getAsShortText() + " " + date.getDayOfMonth();
		this.year = Integer.toString(date.getYear());

		this.setBackgroundColor(Color.WHITE);
		this.setLayoutParams(params);
	}
	
	@Override
	public void onDraw(Canvas canvas) {

		int X = getWidth();
		int Y = getHeight();
		int y;
		canvas.drawLine(100, 0, 100, Y, vLine);
		canvas.drawLine(0, 0, X, 0, border);

		for(int i = 0; i <= 24; i++) {

			y = i * 180;

			canvas.drawText(String.format("%02d", i) + ".00", 50, y + 25, vLine);
			canvas.drawText(day, 50, y + 70, vLine);
			canvas.drawText(monthDay, 50, y + 100, vLine);
			canvas.drawText(year, 50, y + 130, vLine);

			mPath.rewind();
			mPath.setLastPoint(100, y + 90);
			mPath.lineTo(X, y + 90);
			
			canvas.drawPath(mPath, hLine);
			canvas.drawLine(0, y, X, y, hLine);
		}
	}

	public void addView(View child, DateTime startTime, DateTime endTime) {
		DateTime dt = date.toDateTimeAtStartOfDay();
		Minutes m = Minutes.minutesBetween(dt, startTime);
		int height = Minutes.minutesBetween(startTime, endTime).getMinutes() * 3;
		LayoutParams lParams = new LayoutParams(LayoutParams.MATCH_PARENT, height);
		lParams.setMargins(100, m.getMinutes() * 3, 0, 0);
		super.addView(child, lParams);
	}
}